import { Inject, Post } from '@midwayjs/core';
import { CoolController, BaseController } from '@cool-midway/core';
import { CheckDataEntity } from '../../entity/checkData';
import { StudentCheckDataService } from '../../service/checkData';
import { StudentStudentEntity } from '../../entity/student';

/**
 * 后台处方控制器
 */
@CoolController({
  api: ['add', 'delete', 'update', 'info', 'list', 'page'],
  entity: CheckDataEntity,
  service: StudentCheckDataService,
  insertParam: ctx => {
    return {
      operatorId: ctx.admin.userId,
    };
  },
  pageQueryOp: {
    keyWordLikeFields: ['a.remark'],
    fieldEq: ['a.studentId', 'b.name', 'b.phone'],
    where: async ctx => {
      const { studentName, studentPhone } = ctx.request.body;
      const where = [];
      if (studentName) {
        where.push([
          'b.name = :studentName',
          { studentName }
        ]);
        console.log(where);
      }
      if (studentPhone) {
        where.push([
          'b.phone = :studentPhone',
          { studentPhone }
        ]);
      }
      return where;
    },
    addOrderBy: {
        checkTime: 'desc',
    },
    select: [
        'a.*', 
        'b.name as studentName',
        'b.phone as studentPhone',
    ],
    join: [
        {
            entity: StudentStudentEntity,
            alias: 'b',
            condition: 'a.studentId = b.id',
            type: 'leftJoin',
        }
    ],
  },
})
export class AdminStudentCheckDataController extends BaseController {
  @Inject()
  studentCheckDataService: StudentCheckDataService;


  @Post('/cleanEyeAxisData')
  async cleanEyeAxisData() {
    const result = await this.studentCheckDataService.cleanEyeAxisData();
    return this.ok(result);
  }

  @Post('/mergeEyeData')
  async mergeEyeData() {
    const result = await this.studentCheckDataService.mergeEyeData();
    return this.ok(result);
  }
}
